<template>
  <div class="login-account">
    <el-form label-width="80px" :rules="rules" :model="account" ref="formRef">
      <el-form-item label="账号" prop="name">
        <el-input v-model="account.userName" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input show-password v-model="account.password" />
      </el-form-item>
      <el-form-item label="验证码" prop="code">
        <div class="flex">
          <el-input v-model="account.code" />
          <img
            src="http://127.0.0.1:8083/admin/getCode"
            alt="点击刷新"
            onclick="this.src='http://127.0.0.1:8083/admin/getCode?d='+new Date()*1"
          />
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import { ElForm } from 'element-plus'

import { rules } from '../config/account-config'
import { useStore } from 'vuex'

export default defineComponent({
  setup() {
    const account = reactive({
      userName: '',
      password: '',
      code: ''
    })
    const store = useStore()
    const formRef = ref<InstanceType<typeof ElForm>>()

    const loginAction = () => {
      formRef.value?.validate((valid) => {
        if (valid) {
          store.dispatch('login/accountLoginAction', {
            ...account
          })
        }
      })
    }

    return {
      account,
      rules,
      loginAction,
      formRef
    }
  }
})
</script>

<style scoped lang="less">
.flex {
  display: flex;
  img {
    margin-left: 10px;
  }
}
</style>
